<?php

namespace app\admin\controller;

use app\admin\traits\Result;
use think\Controller;
use app\admin\model\ArticleCategory as ACategory;
use think\Request;

class ArticleCategory extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    public function articleCategoryList(){
        if ($this->request->isAjax()){
            $data = [
                'key'=>$this->request->get('key','','trim'),
                'limit' =>$this->request->get('limit','','intval')
            ];

            //$list是对象{}，layui框架数据表要求返回的是数组对象[{},{}]
            //withSearch 搜索器   在模型里定义搜索器 然后在调用withSearch的时候触发
            $list = ACategory::withSearch(['name'],['name'=>$data['key']])->where('status','neq','-1')->order('id','desc')
                            ->paginate($data['limit'],false,['query'=>$data]);
            $user_date = [];

            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date,0,'',['count'=>$list->total()]);

        }
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        //
        if ($this->request->isPost()){
            $data = $this->request->Post();
            if ($data['id']){

                $result = \app\facade\ArticleCategory::edit($data);
            }else{
                $result = \app\facade\ArticleCategory::add($data);
            }
            return $result;
        }else{
            $id = $this->request->get('id','','intval');
            if ($id){
                $list = ACategory::where(['id'=>$id])->find();
                $this->assign('list',$list);
            }
            return $this->fetch();
        }

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //获得id
        $id = $this->request->post('id');
        if (empty($id)){
            $msg = Result::error('参数错误');
        }else{
            $msg = \app\facade\ArticleCategory::del($id);
        }
        return $msg;

    }
}
